package com.java.jvm.loader;

import sun.misc.Launcher;

import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.URL;

/**
 * 获取类加载器对应的类加载目录
 */
public class ClassLoaderDirTests {
    public static void main(String[] args)
            throws Exception{
        //1.获取根类加载器(BootStrapClassLoader)对应的类加载目录
        URL[] urls = Launcher.getBootstrapClassPath().getURLs();
        for(URL url:urls){
            System.out.println(url);
        }
        //2.获取扩展类加载器(ExtClassLoader)对应的类加载目录
        ClassLoader extClassLoader =
                ClassLoader.getSystemClassLoader().getParent();
        Class<? extends ClassLoader> aClass = extClassLoader.getClass();
        final Method getExtDirs = aClass.getDeclaredMethod("getExtDirs");
        getExtDirs.setAccessible(true);
        File[] files=(File[])getExtDirs.invoke(extClassLoader);
        for(File file:files){
            System.out.println(file.getPath());
        }

    }
}
